<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="680px" :title="`${dialogProps.title}角色`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="dialogProps.row!.roleName" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色标识" prop="roleCode">
        <el-input v-model="dialogProps.row!.roleCode" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色类型" prop="categoryId">
        <el-select class="w100" v-model="dialogProps.row!.categoryId">
          <el-option v-for="item in accountType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="comments">
        <el-input type="textarea" v-model="dialogProps.row!.comments" placeholder="请输入" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="RoleDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Operation } from "@/api/interface";
import { accountType } from "@/utils/serviceDict";
import { getDepartMentList } from "@/api/modules/operation";

const rules = reactive({
  roleName: [{ required: true, message: "请输入角色名称" }]
  // roleCode: [{ required: true, message: "请输入角色标识" }],
  // categoryId: [{ required: true, message: "请选择角色类型" }],
  // comments: [{ required: true, message: "请输入备注" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Operation.ResDepartmentList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});

const department = ref([]);

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
  if (dialogProps.value.title === "新增") {
    dialogProps.value.row!.categoryId = 1;
  }
  getDepartMentList({ pageNum: 1, pageSize: 9999 }).then(res => {
    department.value = res.data.list;
  });
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.row.roleName}角色成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
